package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;

import com.laoxuai.util.StringUtils;
import com.music.dao.CollectDao;
import com.music.pojo.Collect;
import com.music.service.CollectService;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectDao collectDao;

    @Override
    public int insert(Integer userId,Integer musicId) {
        try {
            Collect collect=new Collect();
            collect.setUserId(userId);
            collect.setMusicId(musicId);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String curTime=sdf.format(new Date());
            collect.setCreateTime(sdf.parse(curTime));
            return collectDao.insert(collect);
        } catch (SQLException | ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        try {
            return collectDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Collect collect) {
        try {
            return collectDao.update(collect);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Collect getByPrimaryKey(Integer id) {
        try {
            return collectDao.getByPrimaryKey(id);
        } catch (SQLException e) {
        e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Collect> listPage(Collect collect, int page, int size) {
        try {
            return collectDao.listPage(collect, page, size);
        } catch (SQLException throwables) {
        throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Collect> listPageInfo(Collect collect, int page, int size) {
        try {
            return collectDao.listPageInfo(collect, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Collect> selectByExample(Collect collect) {
        Example example = createExmaple(collect);
        try {
            return collectDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Collect collect) {
        Example example = new Example(Collect.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(collect != null){
            if(!StringUtils.isEmpty(collect.getId())){
                criteria.andEqualTo("id", collect.getId());
            }
            if(!StringUtils.isEmpty(collect.getUserId())){
                criteria.andEqualTo("userId", collect.getUserId());
            }
            if(!StringUtils.isEmpty(collect.getMusicId())){
                criteria.andEqualTo("musicId", collect.getMusicId());
            }
            if(!StringUtils.isEmpty(collect.getPlayCount())){
                criteria.andEqualTo("playCount", collect.getPlayCount());
            }
            if(!StringUtils.isEmpty(collect.getCreateTime())){
                criteria.andEqualTo("createTime", collect.getCreateTime());
            }
        }
        return example;
    }

    /**
     * Collect 构建查询对象
     * @param collect
     * @return
     */
    public Example createExample(Collect collect){
        Example example = new Example(Collect.class);
        Example.Criteria criteria = example.createCriteria();
        if(collect != null){
            if(!StringUtils.isEmpty(collect.getId())){
                criteria.andEqualTo("id", collect.getId());
            }
            if(!StringUtils.isEmpty(collect.getUserId())){
                criteria.andEqualTo("userId", collect.getUserId());
            }
            if(!StringUtils.isEmpty(collect.getMusicId())){
                criteria.andEqualTo("musicId", collect.getMusicId());
            }
            if(!StringUtils.isEmpty(collect.getPlayCount())){
                criteria.andEqualTo("playCount", collect.getPlayCount());
            }
            if(!StringUtils.isEmpty(collect.getCreateTime())){
                criteria.andEqualTo("createTime", collect.getCreateTime());
            }
        }
        return example;
    }
}
